<template>
  <div class="user-management-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit($event)"
        >
          添加
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.MatchString"
              clearable
              placeholder="请输入"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      ref="tableSortRef"
      v-loading="listLoading"
      :border="border"
      :data="list"
      :height="height"
      :size="lineHeight"
      :stripe="stripe"
      @selection-change="setSelectRows"
    >
      <el-table-column
        v-for="(item, index) in finallyColumns"
        :key="index"
        align="center"
        :label="item.label"
        :prop="item.prop"
        :sortable="item.sortable"
        width="auto"
      >
        <template slot-scope="scope">
          <span v-if="item.label === 'BOM清单'" @click="setBom(scope.row)">
            <el-button text type="primary">BOM清单</el-button>
          </span>
          <span
            v-if="item.label === '生产历史'"
            @click="handleAddistoryLog(scope.row)"
          >
            <el-button text type="primary">生产历史</el-button>
          </span>
          <span v-else>{{ scope.row[item.prop] }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        show-overflow-tooltip
        width="200"
      >
        <template slot-scope="scope">
          <!-- <el-button text type="primary" @click="handleEdit(row)">
            编辑
          </el-button> -->
          <el-button text type="primary" @click="handleDelete(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
      <template slot="empty">
        <el-empty class="vab-data-empty" description="暂无数据" />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <el-dialog
      :close-on-click-modal="false"
      :visible="dialogVisible"
      title="添加"
      width="30%"
      :before-close="handleClose"
    >
      <el-form :model="form" label-width="100px">
        <el-form-item label="产品编号" :label-width="formLabelWidth">
          <el-input v-model="form.ProducationId" :rows="2" type="text" />
        </el-form-item>
        <el-form-item label="产品名称" :label-width="formLabelWidth">
          <el-input v-model="form.ProducationName" :rows="2" type="text" />
        </el-form-item>
        <el-form-item label="图纸编号" :label-width="formLabelWidth">
          <el-input v-model="form.BluePrintFileId" :rows="2" type="text" />
        </el-form-item>
        <el-form-item label="所属项目" :label-width="formLabelWidth">
          <el-input v-model="form.BelongsToProject" :rows="2" type="textarea" />
        </el-form-item>
        <el-form-item label="检测模板" :label-width="formLabelWidth">
          <el-input v-model="form.CheckTemplate" :rows="2" type="text" />
        </el-form-item>
        <el-form-item label="材质" :label-width="formLabelWidth">
          <el-input v-model="form.Material" :rows="2" type="text" />
        </el-form-item>
        <!-- <el-form-item label="录入时间" :label-width="formLabelWidth">
          <el-date-picker
            v-model="form.CreateTime"
            type="form.name"
            placeholder="录入时间"
          />
        </el-form-item>
        <el-form-item label="录入人" :label-width="formLabelWidth">
          <el-input v-model="form.Creator" :rows="2" type="text" />
        </el-form-item> -->
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="form.Remarks" :rows="2" type="text" />
        </el-form-item>
      </el-form>
      <template slot="footer">
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submit">保存</el-button>
        </span>
      </template>
    </el-dialog>

    <el-dialog
      :close-on-click-modal="false"
      :visible="dialogVisible1"
      title="BOM清单录入"
      width="60%"
      :before-close="handleClose1"
    >
      <el-descriptions direction="vertical" :column="1" border>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user"></i>
            Proudt
          </template>
          <el-tag size="small">编号</el-tag>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user"></i>
            2020-10-1
          </template>
          申请时间
        </el-descriptions-item>
      </el-descriptions>
      <div style="margin-top: 30px">
        <el-descriptions
          title="BOM清单"
          direction="vertical"
          :column="1"
          border
        >
          <el-descriptions-item label="">
            <template slot="label">
              <i class="el-icon-user"></i>
              ProunA
            </template>
            <div>
              <el-table :data="[{}, {}, {}]" style="width: 100%">
                <el-table-column label="工序">
                  <template slot="default">OP10</template>
                </el-table-column>
                <el-table-column label="设备">
                  <template slot="default">
                    <p>VM10</p>
                    <p>VM10</p>
                    <p>VM10</p>
                  </template>
                </el-table-column>
                <el-table-column label="刀具">
                  <template slot="default">
                    <p>10mm锉刀</p>
                    <p>10mm锉刀</p>
                    <p>10mm锉刀</p>
                  </template>
                </el-table-column>
                <el-table-column label="夹具">
                  <template slot="default">T001</template>
                </el-table-column>
                <el-table-column label="毛坯">
                  <template slot="default">T001</template>
                </el-table-column>
              </el-table>
            </div>
          </el-descriptions-item>
        </el-descriptions>
      </div>
      <template slot="footer">
        <span class="dialog-footer">
          <el-button @click="dialogVisible1 = false">取消</el-button>
        </span>
      </template>
    </el-dialog>

    <el-dialog
      :close-on-click-modal="false"
      :visible="dialogVisible2"
      title="生产历史"
      width="60%"
      :before-close="handleClose2"
    >
      <el-table :data="historyData" style="width: 100%">
        <el-table-column label="工单编号" prop="OrderId"></el-table-column>
        <el-table-column label="产品编号" prop="ProductId"></el-table-column>
        <el-table-column label="产品名称" prop="ProductName"></el-table-column>
        <el-table-column label="数量" prop="Num"></el-table-column>
        <el-table-column label="合格数量" prop="QualifiedNum"></el-table-column>
        <el-table-column
          label="不合格数量"
          prop="Unqualified"
        ></el-table-column>
        <el-table-column label="创建者" prop="Creator"></el-table-column>
        <el-table-column label="创建时间" prop="CreateTime"></el-table-column>
      </el-table>
      <template slot="footer">
        <span class="dialog-footer">
          <el-button @click="dialogVisible2 = false">取消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
  import {
    ProductInfo,
    ProductLists,
    ProductInfoDelete,
    HistoryProcessLog,
    BomManagerInfo,
  } from '@/api/product'

  export default {
    data() {
      return {
        TrayInfoData: [],
        form: {
          ProducationId: '',
          ProducationName: '',
          BluePrintFileId: '',
          BelongsToProject: '',
          CheckTemplate: '',
          Material: '',
          Creator: '',
          CreateTime: '',
          Remarks: '',
        },
        radio: 3,
        formLabelWidth: '100px',
        dialogVisible: false,
        dialogVisible1: false,
        dialogVisible2: false,
        tableSortRef: null,
        editRef: null,
        border: true,
        height: this.$baseTableHeight(1),
        stripe: false,
        lineHeight: 'small',
        isFullscreen: false,
        checkList: [
          '产品编号',
          '产品名称',
          '图纸编号',
          '所属项目',
          '检测模板',
          '材质',
          '备注',
          '工艺',
          'BOM清单',
          '生产历史',
        ],
        columns: [
          {
            label: '产品编号',
            prop: 'ProducationId',
            sortable: true,
            disableCheck: true,
          },
          {
            label: '产品名称',
            prop: 'ProducationName',
          },
          {
            label: '图纸编号',
            prop: 'BluePrintFileId',
          },
          {
            label: '所属项目',
            prop: 'BelongsToProject',
          },
          {
            label: '材质',
            prop: 'Material',
          },
          {
            label: '检测模板',
            prop: 'CheckTemplate',
          },
          {
            label: '备注',
            prop: 'Remarks',
          },
          {
            label: '工艺',
            prop: '',
          },
          {
            label: '生产历史',
            prop: '',
          },
          {
            label: 'BOM清单',
            prop: '',
          },
        ],
        list: [],
        imageList: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          MatchString: '',
        },
        title: '添加',
        historyData: [],
      }
    },
    computed: {
      finallyColumns() {
        return this.columns.filter((item) =>
          this.checkList.includes(item.label)
        )
      },
    },
    created() {
      this.fetchData()
    },
    methods: {
      handleAddistoryLog(row) {
        HistoryProcessLog({
          ProductId: row.ProducationId,
          Index: 0,
          Count: 10,
        }).then((res) => {
          this.dialogVisible2 = true
          this.historyData = res.ProductInfo
        })
      },
      setBom(row) {
        BomManagerInfo({ ProductId: row.ProducationId }).then((res) => {
          console.log(res)
          this.dialogVisible1 = true
        })
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      submit() {
        this.form.CreateTime = this.days()
        ProductInfo({ PartNewlyModel: this.form }).then((res) => {
          this.$baseMessage('提交成功', 'success', 'vab-hey-message-success')
          this.dialogVisible = false
          this.fetchData()
        })
      },
      handleEdit(row) {
        if (row.ProducationId) {
          this.title = '编辑'
          this.form = row
        } else {
          this.title = '添加'
        }

        this.dialogVisible = true
      },
      handleDelete(row) {
        console.log('rowrowrow', row.ProducationId)
        this.$baseConfirm('你确定要删除当前项吗', null, async () => {
          const { msg } = await ProductInfoDelete({
            ProductId: row.ProducationId,
          })
          // $baseMessage(msg, 'success', 'vab-hey-message-success')
          await this.fetchData()
        })
        return false
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id).join()
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
              await this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      },
      handleClose() {
        this.dialogVisible = false
      },
      handleClose2() {
        this.dialogVisible2 = false
      },
      handleClose1() {
        this.dialogVisible1 = false
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const params = {
          Index: this.getPage(this.queryForm.pageNo, this.queryForm.pageSize),
          Count: this.queryForm.pageSize,
          MatchString: this.queryForm.MatchString,
        }
        ProductLists(params).then((res) => {
          this.list = res.ProductInfo
          this.total = res.TotalCount
          this.listLoading = false
        })
      },
    },
  }
</script>
